package com.yunxin.core.utils.infra.utils;

import cn.hutool.core.collection.CollUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zero°
 * @since 2023-11-28
 */
public enum ConvertUtils {

    /**
     * 单例
     */
    X;

    /**
     * list 转 string (,号拼接)
     * @param list list
     * @param splicer splicer
     * @return String
     */
    public String fromList(List<?> list, String splicer){
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.stream().map(String::valueOf).collect(Collectors.joining(splicer));
    }

    /**
     * string 转 list<Integer> (,号拼接)
     * @param str str
     * @param splicer splicer
     * @return String
     */
    public List<Integer> toIntegerList(String str, String splicer){
        if(StringUtils.isNotBlank(str)){
            return Arrays.stream(str.split(splicer)).map(Integer::valueOf).toList();
        }
        return Collections.emptyList();
    }

    /**
     * string 转 list<String> (,号拼接)
     * @param str str
     * @param splicer splicer
     * @return String
     */
    public List<String > toStringList(String str, String splicer){
        if(StringUtils.isNotBlank(str)){
            return Arrays.stream(str.split(splicer)).toList();
        }
        return Collections.emptyList();
    }
}
